package com.ruoyi.user.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.user.api.RemoteCarService;
import com.ruoyi.user.api.domain.CheCar;
import com.ruoyi.user.service.ICheCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.CheUserr;
import com.ruoyi.user.service.ICheUserrService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/userr")
public class CheUserrController extends BaseController
{
    @Autowired
    private ICheUserrService cheUserrService;

    @Autowired
    private RemoteUserService remoteUserService;
@Autowired
private ICheCarService iCheCarService;


    /**
     * 通过userid获取详细信息
     * @param userid
     * @return
     */

@GetMapping(value = "/getxiangxi/{id}")
public AjaxResult getxiangxi(@PathVariable("id") Long userid){
    CheUserr cheUserr = new CheUserr();
    cheUserr.setSysUserid(userid);
    List<CheUserr> cheUserrs = cheUserrService.selectCheUserrList(cheUserr);
    CheUserr cheUserr1 = cheUserrs.get(0);
    Long sysUserid = cheUserr1.getSysUserid();
    AjaxResult info = remoteUserService.getInfo(sysUserid, null);
    Object o = info.get(AjaxResult.DATA_TAG);
    Object json = JSON.toJSON(o);
    if (json != null && !json.equals("")) {
        SysUser sysUser = JSON.parseObject(json.toString(), SysUser.class);
        cheUserr1.setSysUser(sysUser);
    }


    return AjaxResult.success(cheUserr1);


}







    /**
     *通过userid查询车辆信息
     * @param usrid
     * @return
     */

    @GetMapping(value = "/getcar/{id}")
    public AjaxResult getcarlist(@PathVariable("id") Long usrid){

        CheUserr cheUserr = new CheUserr();
        cheUserr.setSysUserid(usrid);
        List<CheUserr> cheUserrs = cheUserrService.selectCheUserrList(cheUserr);
        CheUserr cheUserr1 = cheUserrs.get(0);
        Long id = cheUserr1.getId();
        CheCar cheCar = new CheCar();
        cheCar.setUserId(id);
        List<CheCar> cheCars = iCheCarService.selectCheCarList(cheCar);
        return AjaxResult.success(cheCars);
    }



    /**
     * 查询用户列表
     */
    @RequiresPermissions("user:userr:list")
    @GetMapping("/list")
    public TableDataInfo list(CheUserr cheUserr)
    {
        startPage();
        List<CheUserr> list = cheUserrService.selectCheUserrList(cheUserr);
        System.out.println(list+"oooooooooooooooooooooooooooooo");

        for (CheUserr cheUser : list) {
            Long sysUserid = cheUser.getSysUserid();

            AjaxResult infotwo = remoteUserService.getInfo(sysUserid, SecurityConstants.INNER);
            if (infotwo != null) {
                Object o =  infotwo.get(AjaxResult.DATA_TAG);
                Object json = JSON.toJSON(o);
                if (json != null && !json.equals("")) {
                    SysUser sysUser = JSON.parseObject(json.toString(), SysUser.class);
                    cheUser.setSysUser(sysUser);
                }
                if (json == null) {
                    logger.warn("json is null");
                }
            }



        }
        return getDataTable(list);
    }
    @GetMapping("/bySysUser")
    public R<CheUserr> bySysUser(CheUserr cheUserr) {
        CheUserr cheUserr1 = cheUserrService.selectBySysId(cheUserr);
        return R.ok(cheUserr1);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("user:userr:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheUserr cheUserr)
    {
        List<CheUserr> list = cheUserrService.selectCheUserrList(cheUserr);
        ExcelUtil<CheUserr> util = new ExcelUtil<CheUserr>(CheUserr.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @RequiresPermissions("user:userr:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheUserrService.selectCheUserrById(id));
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("user:userr:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody CheUserr cheUserr)
    {

        SysUser sysUser = cheUserr.getSysUser();
//        增加原来的user

        Long add = remoteUserService.add(sysUser, SecurityConstants.INNER);
        if (add==null) {
            System.out.println(1/0);

        }
  cheUserr.setSysUserid(add);


    return toAjax(cheUserrService.insertCheUserr(cheUserr));



    }

    /**
     * 修改用户
     */
    @RequiresPermissions("user:userr:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheUserr cheUserr)
    {
        return toAjax(cheUserrService.updateCheUserr(cheUserr));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("user:userr:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheUserrService.deleteCheUserrByIds(ids));
    }
}
